package com13cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "two13Servlet")
public class two13Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookieArray[]=request.getCookies();
        Cookie noney=null;
        //response.setContentType("text/html");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        String cai=request.getParameter("food");
        System.out.println(cai);;
        for (Cookie c:cookieArray
             ) {
            if (c.getName().equals("2")) {
                if (cai.equals("饺子")) {
                    if ((Integer.valueOf(c.getValue()) - 15)<0)
                    {
                        out.write("余额不足！！！");
                        break;
                    }
                    c.setValue(String.valueOf(Integer.valueOf(c.getValue()) - 15));
                    out.write("本次消费15元，");
                }
                if (cai.equals("面条")) {
                    if ((Integer.valueOf(c.getValue()) - 30)<0)
                    {
                        out.write("余额不足！！！");
                        break;
                    }
                    c.setValue(String.valueOf(Integer.valueOf(c.getValue()) - 30));

                    out.write("本次消费30元，");
                }
                if (cai.equals("包子")) {
                    if ((Integer.valueOf(c.getValue()) - 5)<0)
                    {
                        out.write("余额不足！！！");
                        break;
                    }
                    c.setValue(String.valueOf(Integer.valueOf(c.getValue()) - 5));

                    out.write("本次消费5元，");
                }
                if (cai.equals("披萨")) {
                    if ((Integer.valueOf(c.getValue()) - 50)<0)
                    {
                        out.write("余额不足！！！");
                        break;
                    }
                    c.setValue(String.valueOf(Integer.valueOf(c.getValue()) - 50));

                    out.write("本次消费50元，");
                }
                noney=new Cookie("2",c.getValue());
                out.write("支付成功，"+"当前余额："+c.getValue()+"元");
            }
            else if (c.getName().equals("1"))out.write("用户："+c.getValue()+",");
            System.out.println(c.getName()+c.getValue());
        }
        response.addCookie(noney);

    }
}
